const Base = require('../../base.js');

module.exports = class extends Base {
  async indexAction() {
    let userInfo = await this.session('user_info')
    if (!userInfo) return this.fail(-1, '用户未登录')
    // 获取用户信息
    let user = await this.model('user/index').info(userInfo._id)
    // 获取交易信息
    let dealList = await this.model('deal/index').all(userInfo._id)
    let money = {
      money: 0
    }
    let addType = ['commission', 'deduct', 'charge']
    let reduceType = ['upgrade', 'cash']
    let charge = [], cash = []
    dealList.forEach(item => {
      if (addType.indexOf(item.type) != -1) {
        money.money += item.money
      } else if (reduceType.indexOf(item.type) != -1) {
        money.money -= item.money
      }
      if (item.type == 'charge') charge.push(item)
      if (item.type == 'cash') cash.push(item)
    })
    this.json({
      status: 200, message: 'success',
      data: {
        user,
        money,
        charge,
        cash
      }
    })
  }
};
